﻿namespace Bll.Sales
{
    using Bll.Company;
    using Bll.Stock;
    using IDal.Sales;
    using Model.Company;
    using Model.Sales;
    using Model.Systems;
    using System;
    using System.Data;
    using WinFormSession;
    using DALFactory;
    public class OrdersBll
    {
        private static readonly IOrders dal = DataAccess.CreateOrders();

        public static void Add(OrdersEnt model)
        {
            dal.Add(model);
        }

        public static bool Check(OrdersEnt order)
        {
            return dal.Check(order);
        }

        public static void Delete(int ID)
        {
            dal.Delete(ID);
        }

        public static void FuCheck(OrdersEnt order)
        {
            dal.FuCheck(order);
        }

        public static DataTable GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        public static int GetMaxId(string swhere)
        {
            return dal.GetMaxId(swhere);
        }

        public static OrdersEnt GetModel(int ID)
        {
            return dal.GetModel(ID);
        }

        public static DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetPageList(PageSize, PageIndex, strWhere);
        }

        public static void IsRequestReturn(OrdersEnt order)
        {
            dal.IsRequestReturn(order);
        }

        public static bool ReturnOrder(OrdersEnt order)
        {
            return dal.ReturnOrder(order);
        }

        public static DataTable ReturnTopDataTable(string swhere, int top)
        {
            return dal.ReturnTopDataTable(swhere, top);
        }

        public static bool SalesReturn(int OrdersId)
        {
            bool flag;
            try
            {
                UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
                OrdersEnt model = GetModel(OrdersId);
                if (model.IsBlackOut == 1)
                {
                    return true;
                }
                DataTable list = OrdersPayMentListBll.GetList("OrdersId=" + OrdersId.ToString());
                if (list.Rows.Count > 0)
                {
                    OrderPaymentEnt ent3 = OrderPaymentBll.GetModel(int.Parse(list.Rows[0]["PaymentId"].ToString()));
                    if (ent3.IsCheck == 1)
                    {
                        CompayAccountEnt ent4 = CompayAccountBll.GetModel(ent3.AccountId);
                        if (model.CostMoney > ent4.AccountMoney)
                        {
                            return false;
                        }
                        ent4.AccountMoney -= model.CostMoney;
                        CompayAccountBll.Update(ent4);
                        CompayAccountDetailsEnt ent5 = new CompayAccountDetailsEnt {
                            CheckMan = ent.UserId,
                            CheckTime = DateTime.Now,
                            BuildMoney = ent3.AllMoney,
                            CreateMan = ent.UserId,
                            CreateTime = DateTime.Now,
                            DetailsType = 5,
                            InputDetailsSource = ent3.AccountId,
                            IsCheck = 1,
                            NotesNum = ent3.InvoicingNum,
                            Remark = ent3.Remark,
                            ChainNum = ent.ChainNum
                        };
                        CompayAccountDetailsBll.Add(ent5);
                        ent5.DetailsId = CompayAccountDetailsBll.GetMaxId("ChainNum=" + ent.ChainNum);
                        CompayAccountDetailsBll.Check(ent5);
                    }
                }
                if (model != null)
                {
                    StockBll.SalesReturn(OrdersId);
                    model.IsBlackOut = 1;
                    model.BlackManId = ent.UserId;
                    model.BlackManName = ent.UserName;
                    model.BlackTime = DateTime.Now;
                    UpdateZuoFei(model);
                }
                flag = true;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return flag;
        }

        public static DataTable SearchData(string strWhere)
        {
            return dal.SearchData(strWhere);
        }

        public static DataTable SelectGuestDetials(string swhere)
        {
            return dal.SelectGuestDetials(swhere);
        }

        public static DataTable SelectGuestDetialsTwo(string swhere)
        {
            return dal.SelectGuestDetialsTwo(swhere);
        }

        public static DataTable SelectSendsPresent(string shwere)
        {
            return dal.SelectSendsPresent(shwere);
        }

        public static DataTable SelectSendsPresent(int PageSize, int PageIndex, string strWhere)
        {
            return dal.SelectSendsPresent(PageSize, PageIndex, strWhere);
        }

        public static void Update(OrdersEnt model)
        {
            dal.Update(model);
        }

        public static void UpdateCostMoneyAndSendMoney(OrdersEnt ordersEnt)
        {
            dal.UpdateCostMoneyAndSendMoney(ordersEnt);
        }

        public static void UpdateSaleVag(OrdersEnt ordersEnt)
        {
            dal.UpdateSaleVag(ordersEnt);
        }

        public static bool UpdateZuoFei(OrdersEnt order)
        {
            return dal.UpdateZuoFei(order);
        }
    }
}

